A "shelf" is a persistent, dictionary-like object. The difference
with dbm databases is that the values (not the keys!) in a shelf can
be essentially arbitrary Python objects -- anything that the "pickle"
module can handle. This includes most class instances, recursive data
types, and objects containing lots of shared sub-objects. The keys
are ordinary strings.
To summarize the interface (key is a string, data is an arbitrary
object):
\timport shelve
\td = shelve.open(filename) # open, with (g)dbm filename -- no suffix
\td[key] = data\t# store data at key (overwrites old data if
\t\t\t# using an existing key)
\tdata = d[key]\t# retrieve data at key (raise KeyError if no
\t\t\t# such key)
\tdel d[key]\t# delete data stored at key (raises KeyError
\t\t\t# if no such key)
\tflag = d.has_key(key)\t# true if the key exists
\tlist = d.keys()\t# a list of all existing keys (slow!)
\td.close()\t# close it
Dependent on the implementation, closing a persistent dictionary may
or may not be necessary to flush changes to disk.
'''
'Manage shelves of pickled objects.\n\nA "shelf" is a persistent, dictionary-like object. The difference\nwith dbm databases is that the values (not the keys!) in a shelf can\nbe essentially arbitrary Python objects -- anything that the "pickle"\nmodule can handle. This includes most class instances, recursive data\ntypes, and objects containing lots of shared sub-objects. The keys\nare ordinary strings.\n\nTo summarize the interface (key is a string, data is an arbitrary\nobject):\n\n\timport shelve\n\td = shelve.open(filename) # open, with (g)dbm filename -- no suffix\n\n\td[key] = data\t# store data at key (overwrites old data if\n\t\t\t# using an existing key)\n\tdata = d[key]\t# retrieve data at key (raise KeyError if no\n\t\t\t# such key)\n\tdel d[key]\t# delete data stored at key (raises KeyError\n\t\t\t# if no such key)\n\tflag = d.has_key(key)\t# true if the key exists\n\tlist = d.keys()\t# a list of all existing keys (slow!)\n\n\td.close()\t# close it\n\nDependent on the implementation, closing a persistent dictionary may\nor may not be necessary to flush changes to disk.\n'